package com.guoheng.hazard.sys.service.impl.entpr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.enums.ErrorCodeEnum;
import com.guoheng.hazard.common.enums.ReviewStatusEnum;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.model.dto.entpr.DealExaminationDTO;
import com.guoheng.hazard.sys.model.dto.entpr.SpecialOperationInsertDTO;
import com.guoheng.hazard.sys.model.dto.entpr.SpecialOperationSelectDTO;
import com.guoheng.hazard.sys.model.dto.entpr.SpecialOperationUpdateDTO;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.service.entpr.SpecialOperationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.xml.crypto.Data;

import com.guoheng.hazard.sys.model.entity.entpr.SpecialOperation;
import com.guoheng.hazard.sys.mapper.entpr.SpecialOperationMapper;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类功能描述:　SpecialOperationServiceImpl
 *
 * @author fbl
 * @date 2019-07-20 17:07
 */
@Service
public class SpecialOperationServiceImpl implements SpecialOperationService {

    @Resource
    private SpecialOperationMapper specialOperationMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;


    @Override
    public Result getSpecialOperation(Integer reviewStatus,String entprName, Integer operationType, Long entprId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<SpecialOperationSelectDTO> specialOperationSelectDTOS = new ArrayList<>();

        List<SpecialOperation> specialOperation = specialOperationMapper.getSpecialOperation(reviewStatus,entprName, operationType, entprId);
        for (SpecialOperation operation : specialOperation) {
            SpecialOperationSelectDTO specialOperationSelectDTO = new SpecialOperationSelectDTO();
            BeanUtils.copyProperties(operation,specialOperationSelectDTO);
            // 企业名称
            Long eId = operation.getEntprId();
            EntprBasicInfo entprBasicInfoById = entprBasicInfoMapper.getEntprBasicInfoById(eId);
            specialOperationSelectDTO.setEntprName(entprBasicInfoById.getEntprName());
            specialOperationSelectDTOS.add(specialOperationSelectDTO);
        }

        PageInfo<SpecialOperationSelectDTO> specialOperationPageInfo = new PageInfo<>(specialOperationSelectDTOS);
        return Result.success(specialOperationPageInfo);
    }

    @Override
    public Result updateSpecialOperation(SpecialOperationUpdateDTO specialOperationUpdateDTO) {
        @NotNull Long id = specialOperationUpdateDTO.getId();
        SpecialOperation so = specialOperationMapper.selectByPrimaryKey(id);
        if(so.getReviewStatus().equals(ReviewStatusEnum.REVIEW_STATUS_2.getCode()) || so.getReviewStatus().equals(ReviewStatusEnum.REVIEW_STATUS_3.getCode())){
            return Result.failure(ErrorCodeEnum.SYS_ERR_EXAMINE);
        }
        SpecialOperation specialOperation = new SpecialOperation();
        BeanUtils.copyProperties(specialOperationUpdateDTO,specialOperation);
        specialOperationMapper.updateByPrimaryKeySelective(specialOperation);
        return Result.success();
    }

    @Override
    public Result insertSpecialOperation(SpecialOperationInsertDTO specialOperationInsertDTO) {
        SpecialOperation specialOperation = new SpecialOperation();
        BeanUtils.copyProperties(specialOperationInsertDTO,specialOperation);
        specialOperation.setReviewStatus(ReviewStatusEnum.REVIEW_STATUS_1.getCode());
        specialOperationMapper.insertSelective(specialOperation);
        return Result.success();
    }

    @Override
    public Result getSpecialOperationById(Long id) {
        return Result.success(specialOperationMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result dealSpecialOperationExamine(Long id, DealExaminationDTO dealExaminationDTO) {
        SpecialOperation specialOperation = new SpecialOperation();
        BeanUtils.copyProperties(dealExaminationDTO,specialOperation);
        specialOperation.setId(id);
        specialOperation.setReviewTime(new Date());
        specialOperationMapper.updateByPrimaryKeySelective(specialOperation);
        return Result.success();
    }
}
